import threading
from queue import Queue

import time

__file__ = 'myqueue.py'
__author__ = 'Jerry Liu'
__date__ = '2016-04-22'


_WORKER_THREAD_NUM =2
SHARE_Q = Queue() #构造一个不限制大小的的队列

class MyQueueThread(threading.Thread) :

    def __init__(self, func):
        super(MyQueueThread, self).__init__()
        self.func = func

    def run(self) :
        self.func()

def worker() :

    while not SHARE_Q.empty():
        item = SHARE_Q.get() #获得任务
        print("Processing : ", item)
        time.sleep(1)
        SHARE_Q.put(time.ctime())

def main() :
    global SHARE_Q
    threads = []
    for task in range(5) :  #向队列中放入任务
        print(task)
        SHARE_Q.put(task)

    for i in range(_WORKER_THREAD_NUM) :
        thread = MyQueueThread(worker)
        thread.start()
        threads.append(thread)

    for thread in threads :
        thread.join()

if __name__ == '__main__':
    main()